* Table 9 Panel B

clear all

* Set your directory path here
local root "D:\Understanding_Bank_Payouts"

capture log close
log using "`root'\Code\Logs\Final_code_RCFS\Table9_PanelB.log", replace
display "$S_TIME  $S_DATE"

set more off
set varabbrev off, perm

timer on 1

local finalfile_yearly "`root'\Data\Final\Finalfile_yearly.dta" 

use `finalfile_yearly'
merge m:1 instkey using "`root'\Data\RCFS conditional acceptance\Distance_to_default_2006.dta"
drop _merge
merge 1:1 cusip6 year using "`root'\Data\RCFS conditional acceptance\Institutional_ownership_types_detailed.dta", update
drop if _merge == 2
drop _merge


xtset instkey year

generate avg_logassets_sqY = avg_logassetsY*avg_logassetsY
regress avg_logassets_sqY avg_logassetsY
predict sizesq_orth, residuals
label variable avg_logassetsY "Size (log assets)"
label variable sizesq_orth "Size (log assets) squared, orthogonalized"

generate avg_tassetsW_BN = avg_totassetsWY/(10^9)
generate avg_tassetsW_BN_sq = avg_tassetsW_BN * avg_tassetsW_BN


xtset instkey year, yearly

generate HMDA_to_total3 = (loanvalue + L.loanvalue + L2.loanvalue)/((gross_lns + L.gross_lns + L2.gross_lns)/3)
generate HMDA_to_total2 = (loanvalue + L.loanvalue)/((gross_lns + L.gross_lns)/2)

generate lenientV3 = (loanvalue + L.loanvalue + L2.loanvalue) / (appvalue + L.appvalue + L2.appvalue)
generate lenientC3 = (loancount + L.loancount + L2.loancount) / (appcount + L.appcount + L2.appcount)

generate lenientV2 = (loanvalue + L.loanvalue) / (appvalue + L.appvalue)
generate lenientC2 = (loancount + L.loancount) / (appcount + L.appcount)

foreach var of varlist HMDA_to_total3 lenientV3 lenientC3 {
	generate chg_`var' = D3.`var'
	}

foreach var of varlist HMDA_to_total2 lenientV2 lenientC2 {
	generate chg_`var' = D2.`var'
}


// Make a new variable that captures analyst coverage, including zeroes

generate numest_zeroes = numest
replace numest_zeroes = 0 if numest == .

// Analyst dummy

generate analyst_dummy = 0
replace analyst_dummy = 1 if numest != .

// Generate lag variables to line these up with the 2004-2006 period (since we sort on year 2008 in the quantiles)

xtset instkey year, yearly

generate L2HMDA_to_total3 = L2.HMDA_to_total3 
generate L2lenientV3 = L2.lenientV3

foreach var in fedfunds_balance {
	winsor `var', generate(`var'W) p(0.01)
	label variable `var'W "`var' winsorized at the 1%"
	}

foreach var in fedfunds_balance fedfunds_balanceW {
	by instkey year: egen avg_`var'Y = mean(`var')
	by instkey year: gen `var'4 = `var'[4]  	// The 4th quarter value of the variable in each year
	}

generate avg_fedfunds_repos_scaledWY = (avg_reposWY + avg_fedfunds_balanceWY)/avg_totassetsWY
winsor avg_fedfunds_repos_scaledWY, generate(helper) p(0.01)
drop avg_fedfunds_repos_scaledWY
rename helper avg_fedfunds_repos_scaledWY
label variable avg_fedfunds_repos_scaledWY "Total Fed funds + repos scaled by total assets"

xtset instkey year, yearly

foreach var in avg_tassetsW_BN avg_tassetsW_BN_sq avg_mtbWY lev_fps  ///
	earningsvol avg_int_to_nonintWY pctown_insidersSNL_Y x_npr_countY instown_perc numest_zeroes ///
	avg_stratioY HMDA_to_total3 lenientV3  avg_fedfunds_repos_scaledWY tarpever avg_tieroneratioWY ///
	ded_own_perc qix_own_perc tra_own_perc nr_inst_blk_owners {

			generate L`var' = L.`var'

			}


local rhs1 L.avg_logassetsY L.sizesq_orth ///
L.avg_mtbWY L.avg_roaaWY L.lev_fps L.reteY growth2WY L.avg_tieroneratioWY

local rhs2 L.avg_tassetsW_BN L.avg_tassetsW_BN_sq L.avg_mtbWY L.lev_fps ///
L.earningsvol L.avg_int_to_nonintWY L.pctown_insidersSNL_Y L.instown_perc L.numest_zeroes L.analyst_dummy ///
			L.avg_stratioY L.HMDA_to_total3 L.lenientV3 L.avg_fedfunds_repos_scaledWY L.tarpever L.avg_tieroneratioWY disttodef2006


generate year_2007 = (year == 2007)
generate year_2008 = (year == 2008)

drop if year >= 2010

sum divgrowthY1 tpogrowthY1

file open twowaytests using "Table9_PanelB.txt", write replace

file write twowaytests "Chi-squared tests for differences between coefficients across bins -- one-way" _n(2)


foreach z in Lavg_tieroneratioWY  {

		file write twowaytests "`z'" _n
		file write twowaytests _tab "Returns" _tab "Returns" _tab "ROA" _tab "ROA"  _n
		file write twowaytests "Controls" _tab "No" _tab "Yes" _tab "No" _tab "Yes" _n


		capture drop splitter
		capture drop hisplitter

		bys year: egen splitter = xtile(`z'), nq(2)

		xtset instkey year, yearly

		foreach x of varlist divgrowthY1  {
			
					forvalues j = 1/2 {	
						quietly regress F.returnY `x' c.`x'#c.year_2007 c.`x'#c.year_2008 year_2007 year_2008 ///
						`rhs1' if  splitter == `j'
						gen inreg = e(sample) 
						quietly regress F.returnY `x' c.`x'#c.year_2007 c.`x'#c.year_2008 year_2007 year_2008  ///
						if inreg == 1 & splitter == `j'
						eststo ret_`j'_no
						drop inreg

						quietly regress F.returnY `x' c.`x'#c.year_2007 c.`x'#c.year_2008 year_2007 year_2008 `rhs1' ///
						if splitter == `j'
						eststo ret_`j'_yes
						
						
						quietly regress F.avg_roaaWY `x' c.`x'#c.year_2007 c.`x'#c.year_2008 year_2007 year_2008 `rhs1' ///
						if F.returnY != . &  splitter == `j'
						gen inreg = e(sample) 
						quietly regress F.avg_roaaWY `x' c.`x'#c.year_2007 c.`x'#c.year_2008 year_2007 year_2008 ///
						if inreg == 1 & splitter == `j'
						eststo roa_`j'_no
						drop inreg

						quietly regress F.avg_roaaWY `x' c.`x'#c.year_2007 c.`x'#c.year_2008 year_2007 year_2008  `rhs1' ///
						if F.returnY != . & splitter == `j'
						eststo roa_`j'_yes
			
						}

					

// Low capital ratio			
				suest ret_1_no ret_2_no, cluster(instkey)
				test [ret_1_no_mean]c.`x'#c.year_2007 = [ret_2_no_mean]c.`x'#c.year_2007
				local p_ret_no_2007 = r(p)
				local F_ret_no_2007 = r(chi2)

				test [ret_1_no_mean]c.`x'#c.year_2008 = [ret_2_no_mean]c.`x'#c.year_2008
				local p_ret_no_2008 = r(p)
				local F_ret_no_2008 = r(chi2)

				suest ret_1_yes ret_2_yes, cluster(instkey)
				test [ret_1_yes_mean]c.`x'#c.year_2007 = [ret_2_yes_mean]c.`x'#c.year_2007
				local p_ret_yes_2007 = r(p)
				local F_ret_yes_2007 = r(chi2)

				test [ret_1_yes_mean]c.`x'#c.year_2008 = [ret_2_yes_mean]c.`x'#c.year_2008
				local p_ret_yes_2008 = r(p)
				local F_ret_yes_2008 = r(chi2)


				suest roa_1_no roa_2_no, cluster(instkey)
				test [roa_1_no_mean]c.`x'#c.year_2007 = [roa_2_no_mean]c.`x'#c.year_2007
				local p_roa_no_2007 = r(p)
				local F_roa_no_2007 = r(chi2)

				test [roa_1_no_mean]c.`x'#c.year_2008 = [roa_2_no_mean]c.`x'#c.year_2008
				local p_roa_no_2008 = r(p)
				local F_roa_no_2008 = r(chi2)


				suest roa_1_yes roa_2_yes, cluster(instkey)
				test [roa_1_yes_mean]c.`x'#c.year_2007 = [roa_2_yes_mean]c.`x'#c.year_2007
				local p_roa_yes_2007 = r(p)
				local F_roa_yes_2007 = r(chi2)

				test [roa_1_yes_mean]c.`x'#c.year_2008 = [roa_2_yes_mean]c.`x'#c.year_2008
				local p_roa_yes_2008 = r(p)
				local F_roa_yes_2008 = r(chi2)


				forvalues yr = 2007/2008 {

					file write twowaytests "`yr'" _tab

						foreach perf in ret roa {

							foreach controls in no yes {

								file write twowaytests (round(`F_`perf'_`controls'_`yr'',0.01))
					
								if `p_`perf'_`controls'_`yr'' > 0.1 {
									file write twowaytests "" _tab
									}
								else if `p_`perf'_`controls'_`yr'' > 0.05 {
									file write twowaytests "*" _tab
									}
								else if `p_`perf'_`controls'_`yr'' > 0.01 {
									file write twowaytests "**" _tab
									}		
								else if `p_`perf'_`controls'_`yr'' <= 0.01 {
									file write twowaytests "***"  _tab
									}	
					
								}	

							}	

						

					file write twowaytests _n
							
					}		

				file write twowaytests _n	

			}

		}

file close twowaytests

timer off 1
timer list

display "$S_TIME  $S_DATE"
log close
clear
exit
